import {fakeAccountLogin, fakeUser} from "../services/api";
import {APP_PATH} from "../utils/constant";
import {routerRedux} from 'dva/router';
import {log} from '../utils/log';

export default {
  namespace: 'login',
  state: {
    changeLoginStatus: {},
    user: [1, 2, 3],
    loading: false,
    test: 123,
  },
  reducers: {
    setState(state, {payload}) {
      return {...state, ...payload};
    },
    toggleButton(state, {payload}) {
      let o = Object.assign({}, state)
      o.loading = !state.loading
      return o
    },
  },
  effects: {
    *accountSubmit({payload}, {call, put}) {
      const response = yield call(fakeAccountLogin, payload);
      if(response.status === 'ok') {
        const user = yield call(fakeUser);
        yield put({
          type: 'setState',
          payload: {changeLoginStatus: response, user},
        });
        yield put(routerRedux.push(APP_PATH.example));
      } else {
        throw new Error('用户名密码错误！')
      }
    },
    *changeBg({payload}, {call, put}) {
      log('changebg')
      yield put({
        type: 'toggleButton',
      })
    },
    *pushRouter({payload}, {call, put}) {
      yield put (routerRedux.push(`/app/${payload}`))
    },
    *goBack({call, put}) {
      log('debug back')
      // yield call(routerRedux.)
    },
  },
  subscriptions: {}
};
